package com.yuncheng.spcyApi.flowService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.vo.GetKsKz;
import com.yuncheng.vo.CurrentUserVo;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.busService.BusService;
import com.yuncheng.spcyApi.busService.FlowJobBlsxService;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.flow.*;
import com.yuncheng.spcyApi.constant.schedule.ScheduleConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.flow.SpcyFlowConstant;
import com.yuncheng.spcyApi.scheduleJob.ScheduleJobService;
import com.yuncheng.spcyApi.selectXzxkApi.vo.XzspJkApiJsywbmthParamVo;
import com.yuncheng.spcyApi.selectXzxkApi.vo.XzspJkApiJsywbmthVo;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.service.impl.YcSysUserServiceImpl;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.HnCaUtils;

import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.flow.ReturnFlowVo;
import com.yuncheng.spcyApi.vo.flow.YpscFlowVo;
import com.yuncheng.spcyApi.vo.jcy.RsHcyVo;
import com.yuncheng.spcyApi.vo.spcy.SpcyReturnVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.util.*;

@Component(value = "YpscFlowService")
public class YpscFlowService  extends FlowService {

    private static final Logger log = LoggerFactory.getLogger(YpscFlowService.class);

    @Resource
    private ISysUserFgldService sysUserFgldService;

    @Resource
    @Lazy
    private ISpcySqsxWjjlService spcySqsxWjjlService;
    @Resource
    @Lazy
    private ISpcySqsxXpjlService spcySqsxXpjlService;
    @Resource
    @Lazy
    private ISpcySqsxJcxmService spcySqsxJcxmService;
    @Resource
    @Lazy
    private ISpcyRsHcyService spcyRsHcyService;
    @Resource
    private ISpcySqsxJobService spcySqsxJobService;
    @Resource
    @Lazy
    private ISpcySqsxBaiscService spcySqsxBaiscService;
    @Resource
    @Lazy
    private YpscFlowConstant flowConstant;
    @Resource
    @Lazy
    private YpzcFlowConstant ypzcFlowConstant;
    @Resource
    @Lazy
    private YpltFlowConstant ypltFlowConstant;
    @Resource
    @Lazy
    private YlqxFlowConstant ylqxFlowConstant;
    @Resource
    @Lazy
    private HzpFlowConstant hzpFlowConstant;
    @Resource
    @Lazy
    private JsscbgFlowConstant jsscbgFlowConstant;

    @Resource
    @Lazy
    private SpZgqrFlowConstant spZgqrFlowConstant;

    @Resource
    @Lazy
    private YcSysUserServiceImpl ycSysUserService;
    @Resource
    @Lazy
    private BusService busService;
    @Resource
    private HnCaUtils hnCaUtils;
    @Resource
    @Lazy
    private RckpFlowService rckpFlowService;
    @Resource
    @Lazy
    private YbjcyFlowService ybjcyFlowService;
    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;
    @Resource
    private ScheduleJobService scheduleJobService;

    @Resource
    @Lazy
    private FlowJobBlsxService flowJobBlsxService;

    /**
     * 处理流程
     * @param ypscFlowVo

     * @param jobId
     * @param sqsxid
     * @param submitType
     * @param opinion
     * @return
     */
    public Result handleFlow(YpscFlowVo ypscFlowVo, String jobId, String sqsxid, String submitType, String opinion){
        // 当前流程任务

        SpcySqsxJob nowJob = spcySqsxJobService.getById(jobId);

        if (nowJob == null){
            return Result.error("当前流程任务不存在，请重试");
        }

        // 提交按钮
        if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)) {
            if (nowJob == null) {
                return Result.error("当前流程任务不存在");
            }
        }

        // 任务受理
        if (nowJob.getFhjmc().equals(YpscFlowConstant.HJMC_RWSL)) {
            // 流程环节处理
            Result httpResult = handleNodeName(ypscFlowVo, submitType, nowJob, sqsxid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }
            // 推送受理信息
            if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)) {

                // 是否属于行政审批数据
                boolean isFlag = this.verifySqsxIsXzspData(sqsxid,null);
                if (isFlag){
                    scheduleJobService.pushXzspDataByJkType(sqsxid, ScheduleConstant.Xzsp_Data_Rwsl_Code);
                }
            }

            return httpResult;
        }else if (nowJob.getFhjmc().equals(YpscFlowConstant.HJMC_FAZZ)){

            // 提交验证
            if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)) {
                // 技术审评
                if (StringUtils.isNotBlank(ypscFlowVo.getFscms()) && ypscFlowVo.getFscms().equals(SpcyConstant.SQSX_SCMS_JSSC)) {
                    // 跳环节 综合评定,修改查验完成状态
                    if (StringUtils.isNotBlank(ypscFlowVo.getNextNode()) && ypscFlowVo.getNextNode().equals(YpscFlowConstant.HJMC_ZHPD)) {
                        SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);
                        if (sqsx != null) {
                            sqsx.setFspwczt("是");
                            spcySqsxBaiscService.updateById(sqsx);
                        }
                        busService.updateCyUserGzzt(sqsxid); // 更新人员工作状态
                    }else {
                        List<SpcySqsxXpjl> tyXpXpjlList = busService.getTyXpXpjlList(sqsxid, SpcyConstant.FLOW_TYPE_SP);
                        if (CollectionUtils.isEmpty(tyXpXpjlList)){
                            return Result.error("请选择【同意】选派的专家人员");
                        }
                    }
                } else {
                    // 现场检查
                    // 跳环节 综合评定,修改查验完成状态
                    if (StringUtils.isNotBlank(ypscFlowVo.getNextNode()) && ypscFlowVo.getNextNode().equals(YpscFlowConstant.HJMC_ZHPD)) {
                        SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);
                        if (sqsx != null) {
                            sqsx.setFcywczt("是");
                            spcySqsxBaiscService.updateById(sqsx);
                        }
                        busService.updateCyUserGzzt(sqsxid); // 更新人员工作状态
                    }else {
                        List<SpcySqsxXpjl> tyXpXpjlList = busService.getTyXpXpjlList(sqsxid, SpcyConstant.FLOW_TYPE_CY);
                        if (CollectionUtils.isEmpty(tyXpXpjlList)){
                            return Result.error("请选择【同意】选派的现场检查人员");
                        }
                    }
                }
            }

            // 提交验证
            /*if (StringUtils.isBlank(ypscFlowVo.getNextNode()) && submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)) {
                List<SpcySqsxWjjl> jcfaList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                        .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.jcfa)
                        .eq(SpcySqsxWjjl::getFpid, sqsxid));
                if (CollectionUtils.isEmpty(jcfaList)) {
                    return Result.error("没有进行制作方案，请选制作方案");
                }
            }*/

            if (StringUtils.isNotBlank(ypscFlowVo.getFscms()) && ypscFlowVo.getFscms().equals(SpcyConstant.SQSX_SCMS_JSSC)){
                // 技术审评
                // 提交验证
                /*if (StringUtils.isBlank(ypscFlowVo.getNextNode()) && submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)) {
                    List<SpcySqsxWjjl> jcfaList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                            .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.spfa)
                            .eq(SpcySqsxWjjl::getFpid, sqsxid));
                    if (CollectionUtils.isEmpty(jcfaList)) {
                        return Result.error("没有进行制作审评方案，请选制作审评方案");
                    }
                }*/
            }else {
                // 现场检查
                // 提交验证
                if (StringUtils.isBlank(ypscFlowVo.getNextNode()) && submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)) {
                    List<SpcySqsxWjjl> jcfaList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                            .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.jcfa)
                            .eq(SpcySqsxWjjl::getFpid, sqsxid));
                    if (CollectionUtils.isEmpty(jcfaList)) {
                        return Result.error("没有进行制作检查方案，请选制作检查方案");
                    }
                }
            }

            // 流程环节处理
            Result httpResult = handleNodeName(ypscFlowVo, submitType, nowJob, sqsxid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }
        }else if (nowJob.getFhjmc().equals(YpscFlowConstant.HJMC_FASP)){
            // 提交验证
            if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)) {
                List<SpcySqsxXpjl> tyXpXpjlList = busService.getTyXpXpjlList(sqsxid, SpcyConstant.FLOW_TYPE_CY);
                if (CollectionUtils.isEmpty(tyXpXpjlList)){
                    return Result.error("请选择【同意】选派的人员");
                }

                List<SpcySqsxWjjl> jcfaList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                        .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.jcfa)
                        .eq(SpcySqsxWjjl::getFpid, sqsxid));
                if (CollectionUtils.isEmpty(jcfaList)) {
                    return Result.error("没有进行制作检查方案，请选制作检查方案");
                }
                for (SpcySqsxWjjl wjjl : jcfaList) {
                    if (StringUtils.isNotBlank(wjjl.getFwjlx()) && !wjjl.getFwjlx().equals(".pdf")) {
                        return Result.error("检查方案没有生成PDF，请先生成PDF并盖章");
                    } else if (wjjl.getFwjlx().equals(".pdf")) {
                        if (wjjl.getFgzzt().equals("否")) {
                            // 没有盖章
                            hnCaUtils.fileCaByGJZ(wjjl.getId()); // 文件盖章
                        }
                    } else {
                        continue;
                    }
                }

//                busService.sendSmsAndEmailToHcyGcyQy(sqsxid); // 发检查员、观察员、企业通知相关的函
            }

            // 流程环节处理
            Result httpResult = handleNodeName(ypscFlowVo, submitType, nowJob, sqsxid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }
        }else if (nowJob.getFhjmc().equals(YlqxFlowConstant.HJMC_SP_FASP)){
            // 提交验证
            if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)) {
                List<SpcySqsxXpjl> tyXpXpjlList = busService.getTyXpXpjlList(sqsxid, SpcyConstant.FLOW_TYPE_SP);
                if (CollectionUtils.isEmpty(tyXpXpjlList)){
                    return Result.error("请选择【同意】选派的人员");
                }

                List<SpcySqsxWjjl> jcfaList = spcySqsxWjjlService.list(new LambdaQueryWrapper<SpcySqsxWjjl>()
                        .eq(SpcySqsxWjjl::getFwjlx1, WjmlConstant.spfa)
                        .eq(SpcySqsxWjjl::getFpid, sqsxid));
                if (CollectionUtils.isEmpty(jcfaList)) {
                    return Result.error("没有进行制作审评方案，请选制作审评方案");
                }
                for (SpcySqsxWjjl wjjl : jcfaList) {
                    if (StringUtils.isNotBlank(wjjl.getFwjlx()) && !wjjl.getFwjlx().equals(".pdf")) {
                        return Result.error("审评方案没有生成PDF，请先生成PDF并盖章");
                    } else if (wjjl.getFwjlx().equals(".pdf")) {
                        if (wjjl.getFgzzt().equals("否")) {
                            // 没有盖章
                            hnCaUtils.fileCaByGJZ(wjjl.getId()); // 文件盖章
                        }
                    } else {
                        continue;
                    }
                }

//                busService.sendSmsAndEmailToZjQy(sqsxid); // 发专家/企业 相关函、短信和邮箱
            }

            // 流程环节处理
            Result httpResult = handleNodeName(ypscFlowVo, submitType, nowJob, sqsxid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }
        }else if (nowJob.getFhjmc().equals(YpscFlowConstant.HJMC_XCJC)){
            // 现场检查
            // 流程环节处理
            Result httpResult = handleNodeName(ypscFlowVo, submitType, nowJob, sqsxid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }

            if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)) {

            }
        }else if (nowJob.getFhjmc().equals(YpscFlowConstant.HJMC_ZHPD)){

            if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)){
                this.isExistJsscbgFlow(sqsxid); // 判断-技术审查报告流程是否结束
                this.isExistZgqrFlow(sqsxid); // 判断-整改确认流程是否结束
            }

            // 综合评定
            // 流程环节处理
            Result httpResult = handleNodeName(ypscFlowVo, submitType, nowJob, sqsxid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }
            // 审查结论信息
            if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)) {

            }

        }else if (nowJob.getFhjmc().equals(YpscFlowConstant.HJMC_YSQR)){

            // 验证-行政审批-数据-移送环节-是否保存经办人
            if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)){
               this.verifySqsxIsSaveYsr(sqsxid);
            }

            // 移送确认
            // 流程环节处理
            Result httpResult = handleNodeName(ypscFlowVo, submitType, nowJob, sqsxid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }
            if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)) {
                SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);

                if (sqsx != null) {
                    sqsx.setFlczt(flowConstant.HJMC_LCJS);
                    sqsx.setFhfzt(HfrwFlowConstant.HFZT_WHF);
                    sqsx.setFyssj(new Date());
                    spcySqsxBaiscService.updateById(sqsx);
                }

                // 是否属于行政审批数据
                boolean isFlag = this.verifySqsxIsXzspData(sqsxid,sqsx);
                if (isFlag){
                    scheduleJobService.pushXzspDataByJkType(sqsxid,ScheduleConstant.Xzsp_Data_Fjcl_Code);
                    scheduleJobService.pushXzspDataByJkType(sqsxid,ScheduleConstant.Xzsp_Data_Lzjl_Code);
                    scheduleJobService.pushXzspDataByJkType(sqsxid,ScheduleConstant.Xzsp_Data_Scjl_Code);
                }

                // 定时任务-删除启动minio存储，删除本地文件
                scheduleJobService.deleteSqsxLocalWjjlSchedule(sqsxid);
            }
        }else {
            Result httpResult = handleNodeName(ypscFlowVo, submitType, nowJob, sqsxid, opinion);
            if (!httpResult.isSuccess()){
                return httpResult;
            }
        }
        return Result.ok("操作成功");
    }

    /**
     * 流程环节处理
     * @param sqsxId 申请信息id
     * @param ypscFlowVo 需要修改的字段内容
     * @param job 流程任务job
     * @param opinion 办理意见
     * @param submitType 提交、保存类型
     * @return true(),false(提示信息)
     */
    public Result handleNodeName(YpscFlowVo ypscFlowVo,String submitType, SpcySqsxJob job, String sqsxId, String opinion){
        HashMap<String, Object> map = ypscFlowVo.getMap();
        String dbrid = ypscFlowVo.getDbrid();// 下环节待办人
        String smgflag = ypscFlowVo.getSmgflag(); // 短信发送标志
        String fscms = ypscFlowVo.getFscms(); // 提交的是那种模式
        String smsContent = ypscFlowVo.getSmsContent(); // 发送短信内容
        String nextNode = ypscFlowVo.getNextNode(); // 下个环节

        Result res = null;

        // 保存按钮
        if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SAVE)){
            res = busService.updateSqsxBasic(map, sqsxId, job);
            if (!res.isSuccess()){
                return res;
            }else {
                return Result.ok("保存成功");
            }
        }

        // 提交按钮
        if (submitType.equals(YpscFlowConstant.SUBMIT_TYPE_SUBMIT)){

            res = busService.updateSqsxBasic(map, sqsxId, job);

            if (!res.isSuccess()){
                return res;
            }

            // 流程任务id
            String jobId = "";
            if (job != null){
                jobId = job.getId();
            }

//            String jobNodeName = busService.getJobNodeName(jobId); // 获取环节名称
            if (job == null){
                throw new BusinessException("当前流程任务不存在！");
            }
            if (StringUtils.isBlank(job.getFhjmc())){
                throw new BusinessException("当前流程任务环节名称丢失！");
            }
            String jobNodeName = job.getFhjmc();
//
            // 主办人保存 任务受理
            if (StringUtils.isNotBlank(jobNodeName) && jobNodeName.equals(YpscFlowConstant.HJMC_RWSL)){
                SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxId);
                if (sqsx != null){
                    sqsx.setFzbr(dbrid);
                    String zbrmc = busService.getZbrmcByZbrId(dbrid);
                    sqsx.setFjbrmc(zbrmc);
                    spcySqsxBaiscService.updateById(sqsx);
                }
            }

            // 主办人保存 任务分配
            if (StringUtils.isNotBlank(jobNodeName) && jobNodeName.equals(YlqxFlowConstant.HJMC_RWFP)){
                SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxId);
                if (sqsx != null){
                    sqsx.setFzbr(dbrid);

                    String zbrmc = busService.getZbrmcByZbrId(dbrid);
                    sqsx.setFjbrmc(zbrmc);

                    spcySqsxBaiscService.updateById(sqsx);
                }
            }

            // 提交流程任务,待办人是否为空，指定待办人
            if (StringUtils.isBlank(dbrid)) {
                // 按审查模式-提交-流程任务
                res = this.passJobScms(sqsxId, jobId, opinion, nextNode,"", fscms);
            }else {
                // 按审查模式-提交-流程任务
                res = this.passJobScms(sqsxId,jobId,opinion,nextNode,dbrid,fscms);
            }
            if (!res.isSuccess()){
                return res;
            }else {
                // 接收返回的流程任务，如果返回的流程任务id不相等，发送短信
                /*
                SpcySqsxJob nowJob = (SpcySqsxJob)res.getResult();
                if (nowJob != null && !jobId.equals(nowJob.getId()) && StringUtils.isNotBlank(smgflag) && smgflag.equals("是")){
                    busService.sendSmsToPassJob(nowJob);
                }*/
                // 发送下环节短信
                if (StringUtils.isNotBlank(smgflag) && (smgflag.equals("是") || smgflag.equals("true"))){
                    busService.sendSmsToNextJob(jobId,smsContent);
                }
            }
        }

        return Result.ok("提交成功");
    }

    /**
     * 判断申请-是否为行政审批-数据
     */
    public boolean verifySqsxIsXzspData(String sqsxid,SpcySqsxBasic sqsxBasic){
        SpcySqsxBasic sqsx = null;
        if (sqsxBasic != null){
            sqsx = sqsxBasic;
        }else {
            sqsx = spcySqsxBaiscService.getById(sqsxid);
        }

        if (sqsx == null){
            return false;
        }
        if (StringUtils.isBlank(sqsx.getFxzspzt())){
            return false;
        }

        if (sqsx.getFxzspzt().equals(SpcyConstant.SQSX_BASIC_XZSPZT_YES)){
            return true;
        }

        return false;
    }

    /**
     * 验证-行政审批-数据-移送环节-是否保存经办人
     */
    public void verifySqsxIsSaveYsr(String sqsxid){

        SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);
        if (sqsx != null && StringUtils.isNotBlank(sqsx.getFxzspzt())
                && sqsx.getFxzspzt().equals(SpcyConstant.SQSX_BASIC_XZSPZT_YES)){
            if (StringUtils.isBlank(sqsx.getFxzspysrid())){
                throw new BusinessException("请先选择业务部门经办人！");
            }
        }
    }

    /**
     * 新增申请事项-任务受理环节
     * @param ypscFlowVo 需要修改的字段内容
     * @param sqsxid 申请事项id
     * @return true(流程任务job),false(提示信息)
     */
    public Result addRwslJob(YpscFlowVo ypscFlowVo,String sqsxid){
        HashMap<String, Object> map = ypscFlowVo.getMap();
        SpcySqsxBasic nowBasic = spcySqsxBaiscService.getById(sqsxid); // 当前申请事项

        CurrentUserVo currentUserVo = GetCurrentUserUtils.getCurrentUser();
        SpcySqsxBasic basic = new SpcySqsxBasic();
        basic.setId(sqsxid);
        basic.setFsxsqjbxxid(sqsxid);
        basic.setFslr(currentUserVo.getName());
        if (StringUtils.isNotBlank(ypscFlowVo.getDbrid())){
            basic.setFzbr(ypscFlowVo.getDbrid());
        }
        // 创建流程
        Result result = this.initJob(sqsxid, currentUserVo);
        if (!result.isSuccess()){
            return Result.error(result.getMessage());
        }else {
            if (nowBasic == null) {
                spcySqsxBaiscService.save(basic);
            }
            Result httpResult = busService.updateSqsxBasic(map, sqsxid, null);
            if (!httpResult.isSuccess()){
                return httpResult;
            }
        }
        SpcySqsxJob job = null;
        if (result.isSuccess()){
            job = (SpcySqsxJob) result.getResult();
        }
        return Result.ok(job);
    }

    /**
     * 判断 技术审查报告-流程是否存在，存在无法提交
     */
    public void isExistJsscbgFlow(String sqsxid){
        // 判断是否存在，技术审查报告流程
        List<SpcySqsxJob> isExistList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFlcmc, jsscbgFlowConstant.getFlowName())
                .eq(SpcySqsxJob::getFpid, sqsxid)
                .and(w -> w.eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_DB).or().eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_TH)));
        if (CollectionUtils.isNotEmpty(isExistList)){
            throw new BusinessException("技术审查报告-子流程还没有完成，无法提交当前流程环节。");
        }

    }

    /**
     * 判断 整改确认流程-是否存在，存在无法提交
     */
    public void isExistZgqrFlow(String sqsxid){
        /*List<SpcySqsxJob> isExistList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFlcmc, spZgqrFlowConstant.getFlowName())
                .eq(SpcySqsxJob::getFpid, sqsxid)
                .and(w -> w.eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_DB).or().eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_TH)));
        if (CollectionUtils.isNotEmpty(isExistList)){
            throw new BusinessException("整改确认-子流程还没有完成，无法提交当前流程环节。");
        }*/
    }

    /**
     * 终止审评查验主流程环节
     * @return
     */
    public void endSpcyFlowJob(String sqsxid,String opinion){
        List<SpcySqsxJob> jobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFpid, sqsxid)
                .ne(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                .like(SpcySqsxJob::getFlcmc, YpscFlowConstant.LCMC_LIKE_SPCYLC));

        if (CollectionUtils.isNotEmpty(jobList)){
            for (SpcySqsxJob job : jobList){
                job.setFbllx(SpcyConstant.BLLX_BL);
                job.setFblzt(SpcyConstant.BLZT_YB);
                job.setFblyj(opinion);
                job.setFblsj(new Date());
                job.setFblr(job.getFdbr());
                spcySqsxJobService.updateById(job);
            }
        }

        // 办结撤件申请
        List<SpcySqsxJob> qycjsqJobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFpid, sqsxid)
                .eq(SpcySqsxJob::getFhjmc, QycjsqFlowConstant.NODENAME)
                .ne(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                .like(SpcySqsxJob::getFlcmc, QycjsqFlowConstant.FLOW_NAME));
        if (CollectionUtils.isNotEmpty(qycjsqJobList)){
            for (SpcySqsxJob job : qycjsqJobList){
                job.setFbllx(SpcyConstant.BLLX_BL);
                job.setFblzt(SpcyConstant.BLZT_YB);
                job.setFblyj(opinion);
                job.setFblsj(new Date());
                job.setFblr(job.getFdbr());
                spcySqsxJobService.updateById(job);
            }
        }

    }

    // 2023-3-25 按模式提交流程
    public Result passJobScms(String sqsxid,String jobId,String opinion,String nextNode,String nextNodeDbrIds,String submitScms){
        SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);
        String fscms = sqsx.getFscms(); // 审查模式
        String cyfazt = sqsx.getFcyfazt(); // 查验方案状态
        String spfazt = sqsx.getFspfazt(); // 审评方案状态

        if (fscms.equals(SpcyConstant.SQSX_SCMS_XCJC)){
            // 现场检查
            Result result = passJobScmsXcjc(sqsx, sqsxid, jobId, opinion, nextNode,nextNodeDbrIds);
            if (!result.isSuccess()){
                return result;
            }
        }else if (fscms.equals(SpcyConstant.SQSX_SCMS_JSSC)){
            // 技术审查
            Result result = passJobScmsJssp(sqsx, sqsxid, jobId, opinion, nextNode,nextNodeDbrIds);
            if (!result.isSuccess()){
                return result;
            }
        }else {
            // 现场检查+技术审查
            // 应该是查验和审评都提交之后才完结当前job，如果其中一个没完成就不结束

            // 提交-现场检查
            SpcySqsxJob job = spcySqsxJobService.getById(jobId);
            if (job == null){
                return Result.error("当前流程任务不存在，请重试");
            }
            CurrentUserVo currentUserVo = GetCurrentUserUtils.getCurrentUser();
            if (currentUserVo == null){
                return Result.error("登录失效，请重新登录");
            }
            String nowNode = job.getFhjmc(); // 当前环节名称

            // 方案制作环节-分开提交
            if (nowNode.equals(YpscFlowConstant.HJMC_FAZZ)){
                // 方案制作环节
                Result result = this.passJobScmsXcjcAndJsspByFazz(sqsx, submitScms, spfazt, cyfazt, sqsxid, jobId, opinion, nextNode,nextNodeDbrIds);
                if (!result.isSuccess()){
                    return result;
                }
            }else if (nowNode.equals(YpscFlowConstant.HJMC_XCJC)){
                // 现场检查
                Result result = this.passJobScmsXcjcAndJsspByXcjc(sqsx, sqsxid, jobId, opinion, nextNodeDbrIds);
                if (!result.isSuccess()){
                    return result;
                }
            }else if (nowNode.equals(YpscFlowConstant.HJMC_JSSP)){
                // 技术审评
                Result result = this.passJobScmsXcjcAndJsspByJssp(sqsx, sqsxid, jobId, opinion, nextNodeDbrIds);
                if (!result.isSuccess()){
                    return result;
                }
            }else if (nowNode.equals(YlqxFlowConstant.HJMC_ZHSC)){
                // 综合审查
                Result result = this.passJobScmsXcjcAndJsspByZhsc(sqsx, sqsxid, jobId, opinion, nextNodeDbrIds);
                if (!result.isSuccess()){
                    return result;
                }
            }else {
                if (nowNode.equals(YpscFlowConstant.HJMC_FASP)) {
                    // 查验方案审批
                    nextNodeDbrIds = busService.getDbrXcjc(sqsxid);

                    // 检查项目库
                    spcySqsxJcxmService.fzRsJcxmToSqsxJcxm(sqsxid);

                    // 初始化-日常考评流程
//                    rckpFlowService.initJob(sqsxid,GetCurrentUserUtils.getCurrentUser());

                    // 初始化-预备检查员流程
//                    ybjcyFlowService.joinYbJcyJob(sqsxid);

                } else if (nowNode.equals(YpscFlowConstant.HJMC_SP_FASP)) {
                    // 审评方案审批
                    nextNodeDbrIds = busService.getDbrJssp(sqsxid);

                    // 检查项目库
                    spcySqsxJcxmService.fzRsJcxmToSqsxJcxm(sqsxid);
                }
                // 不是方案制作环节,直接提交
                Result result = this.passJobScmsXcjcAndJsspByQtHj(sqsxid, jobId, opinion, nowNode, nextNodeDbrIds);
                if (!result.isSuccess()){
                    return result;
                }
            }
        }

        return Result.ok("提交成功");
    }

    // 现场检查模式-提交流程
    public Result passJobScmsXcjc(SpcySqsxBasic sqsx,String sqsxid,String jobId,String opinion,String nextNode,String nextNodeDbrIds){
        String jobNodeName = busService.getJobNodeName(jobId);

        // 查验方案审批
        if (jobNodeName.equals(YpscFlowConstant.HJMC_FASP)) {
            nextNodeDbrIds = busService.getDbrXcjc(sqsxid);

            // 检查项目库
            spcySqsxJcxmService.fzRsJcxmToSqsxJcxm(sqsxid);

            // 初始化-日常考评流程
            Result result = rckpFlowService.initJob(sqsxid, GetCurrentUserUtils.getCurrentUser());
            if (!result.isSuccess()){
                throw new BusinessException(result.getMessage());
            }

            // 初始化-预备检查员
            ybjcyFlowService.joinYbJcyJob(sqsxid);
        }
        // 现场检查
        if (jobNodeName.equals(YpscFlowConstant.HJMC_XCJC)){
            busService.updateCyUserGzzt(sqsxid); // 更新人员工作状态

            sqsx.setFcywczt("是");
            spcySqsxBaiscService.updateById(sqsx);
        }

        Result result = null;
        if (StringUtils.isBlank(nextNode)) {
            result = this.passJob(sqsxid, jobId, opinion, "", nextNodeDbrIds);
        }else {
            result = this.passJob(sqsxid, jobId, opinion, nextNode, nextNodeDbrIds);
        }
        if (!result.isSuccess()){
            return result;
        }else {
            if (jobNodeName.equals(YpscFlowConstant.HJMC_FAZZ)) {
                sqsx.setFcyfazt("是");
                spcySqsxBaiscService.updateById(sqsx);
            }
        }
        return Result.ok("success");
    }

    // 技术审评模式-提交流程
    public Result passJobScmsJssp(SpcySqsxBasic sqsx,String sqsxid,String jobId,String opinion,String nextNode,String nextNodeDbrIds){

        if (sqsx != null && StringUtils.isNotBlank(sqsx.getFspzjlczt()) && sqsx.getFspzjlczt().equals(SpcyZjFlowConstant.SP_ZJ_FLOW_ZT)){
            return Result.error("提交失败，请先完成专家函审/会审相关流程。");
        }

        if (sqsx != null && StringUtils.isNotBlank(sqsx.getFspbzlczt()) && sqsx.getFspbzlczt().equals(SpcyZjFlowConstant.SP_Bz_FLOW_ZT)){
            return Result.error("提交失败，请先完成补正流程。");
        }

        String ywflCode = busService.getYwflCode(sqsxid);
        FlowConstant flowConstant1 = SpcyConstant.getFlowConstant(ywflCode);

        String jobNodeName = busService.getJobNodeName(jobId);

        // 审评方案审批
        if (jobNodeName.equals(YpscFlowConstant.HJMC_SP_FASP)) {
            nextNodeDbrIds = busService.getDbrJssp(sqsxid);

            // 检查项目库
            spcySqsxJcxmService.fzRsJcxmToSqsxJcxm(sqsxid);

            // 初始化-日常考评流程
            rckpFlowService.initJob(sqsxid,GetCurrentUserUtils.getCurrentUser());

            // 初始化-预备检查员流程
            ybjcyFlowService.joinYbJcyJob(sqsxid);
        }
        // 技术审评
        if (jobNodeName.equals(YpscFlowConstant.HJMC_JSSP)){
            busService.updateSpUserGzzt(sqsxid); // 更新人员工作状态

        }

        Result result = null;
        if (StringUtils.isBlank(nextNode)) {
            String spNextNodeName = flowConstant1.getSpNextNodeName(jobNodeName); // 审评下环节
            result = this.passJob(sqsxid, jobId, opinion, spNextNodeName, nextNodeDbrIds);
        }else {
            result = this.passJob(sqsxid, jobId, opinion, nextNode, nextNodeDbrIds);
        }
        if (!result.isSuccess()){
            return result;
        }else {
            if (StringUtils.isNotBlank(nextNode) && nextNode.equals(YlqxFlowConstant.HJMC_ZHSC)){
                sqsx.setFspwczt("是");
                spcySqsxBaiscService.updateById(sqsx);
            }
            if (jobNodeName.equals(YpscFlowConstant.HJMC_FAZZ)) {
                sqsx.setFspfazt("是");
                spcySqsxBaiscService.updateById(sqsx);
            }
        }
        return Result.ok("success");
    }

    // 现场检查模式+技术审评模式-提交流程-方案制作
    public Result passJobScmsXcjcAndJsspByFazz(SpcySqsxBasic sqsx,String submitScms,String spfazt,String cyfazt,String sqsxid,String jobId,String opinion,String nextNode,String nextNodeDbrIds){
        String ywflCode = busService.getYwflCode(sqsxid);
        FlowConstant flowConstant1 = SpcyConstant.getFlowConstant(ywflCode);
        // 提交的是 - 现场检查
        if (StringUtils.isBlank(submitScms) || submitScms.equals(SpcyConstant.SQSX_SCMS_XCJC)){

            boolean isOverCurrentJob =false;
            if ( spfazt.equals("是")) {
                isOverCurrentJob =true;
            }

            Result result = null;
            // 是否提交到指定环节，false不指定
            if (StringUtils.isBlank(nextNode)) {
                String jobNodeName1 = busService.getJobNodeName(jobId); // 当前环节
                String nextNodeName1 = flowConstant1.getNextNodeName(jobNodeName1); // 查验-下环节名称
                result = this.passJob(flowConstant1, sqsxid, jobId, opinion, nextNodeName1, nextNodeDbrIds, isOverCurrentJob);// 创建流程任务
            }else {
                boolean isSubmitCreate = false; // 是否创建下流程

                // 提交到指定环节
                if (nextNode.equals(YlqxFlowConstant.HJMC_ZHSC)){

                    // 如果审评完成了，就不需要创建新的流程
                    String fspwczt = sqsx.getFspwczt(); // 审评完成状态
                    if (fspwczt.equals("是")){
                        SpcySqsxJob job = spcySqsxJobService.getById(jobId);
                        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
                        if (currentUser == null){
                            return Result.error("登录失效，请重新登录");
                        }

                        Result result2 = this.doJob(job, currentUser, opinion); // 办理当前流程
                        result = result2;
                        if (!result2.isSuccess()){
                            return result2;
                        }else {
                            sqsx.setFcywczt("是");
                            spcySqsxBaiscService.updateById(sqsx);

                            // 综合评定环节-更新时间
                            this.updateZhpdJobFssj(sqsxid);
                        }
                    }else {
                        isSubmitCreate = true;
                    }
                }else {
                    isSubmitCreate = true;
                }

                if (isSubmitCreate){
                    result = this.passJob(flowConstant1, sqsxid, jobId, opinion, nextNode, nextNodeDbrIds, isOverCurrentJob);// 创建流程任务
                }
            }
            if (!result.isSuccess()) {
                return result;
            } else{
                sqsx.setFcyfazt("是");
                spcySqsxBaiscService.updateById(sqsx);
            }

        }

        // 提交的是 - 技术审评
        if (submitScms.equals(SpcyConstant.SQSX_SCMS_JSSC)){
            if (sqsx != null && StringUtils.isNotBlank(sqsx.getFspzjlczt()) && sqsx.getFspzjlczt().equals(SpcyZjFlowConstant.SP_ZJ_FLOW_ZT)){
                return Result.error("提交失败，请先完成专家函审/会审相关流程。");
            }

            if (sqsx != null && StringUtils.isNotBlank(sqsx.getFspbzlczt()) && sqsx.getFspbzlczt().equals(SpcyZjFlowConstant.SP_Bz_FLOW_ZT)){
                return Result.error("提交失败，请先完成补正流程。");
            }

            boolean isOverCurrentJob =false;
            if ( cyfazt.equals("是")) {
                isOverCurrentJob =true;
            }

            Result result = null;
            // 是否提交到指定环节，false不指定
            if (StringUtils.isBlank(nextNode)) {
                String jobNodeName1 = busService.getJobNodeName(jobId); // 当前环节
                String nextNodeName1 = flowConstant1.getSpNextNodeName(jobNodeName1); // 审评-下环节名称
                result = this.passJob(flowConstant1, sqsxid, jobId, opinion, nextNodeName1, nextNodeDbrIds, isOverCurrentJob);// 创建流程任务

                if (result.isSuccess()) {
                    if (nextNode.equals(YlqxFlowConstant.HJMC_ZHSC)) {
                        sqsx.setFspwczt("是");
                        spcySqsxBaiscService.updateById(sqsx);
                    }
                }
            }else {

                boolean isSubmitCreate = false; // 是否创建下流程

                // 提交到指定环节
                if (nextNode.equals(YlqxFlowConstant.HJMC_ZHSC)){
                    // 如果查验完成了，就不需要创建新的流程
                    String fcywczt = sqsx.getFcywczt(); // 查验完成状态
                    if (fcywczt.equals("是")){
                        SpcySqsxJob job = spcySqsxJobService.getById(jobId);
                        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
                        if (currentUser == null){
                            return Result.error("登录失效，请重新登录");
                        }
                        Result result2 = this.doJob(job, currentUser, opinion); // 办理当前流程
                        result = result2;
                        if (!result2.isSuccess()){
                            return result2;
                        }else {
                            sqsx.setFspwczt("是");
                            spcySqsxBaiscService.updateById(sqsx);

                            // 综合评定环节-更新时间
                            this.updateZhpdJobFssj(sqsxid);
                        }
                    }else {
                        isSubmitCreate = true;
                    }
                }else {
                    isSubmitCreate = true;
                }

                if (isSubmitCreate) {
                    result = this.passJob(flowConstant1, sqsxid, jobId, opinion, nextNode, nextNodeDbrIds, isOverCurrentJob);// 创建流程任务
                }
            }
            if (!result.isSuccess()) {
                return result;
            } else{
                sqsx.setFspfazt("是");
                spcySqsxBaiscService.updateById(sqsx);
            }
        }
        return Result.ok("success");
    }

    // 更新综合评定-环节-发送时间
    public void updateZhpdJobFssj(String sqsxid){
        // 综合评定环节-更新时间
        List<SpcySqsxJob> zhpdList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFpid, sqsxid)
                .eq(SpcySqsxJob::getFhjmc, YpscFlowConstant.HJMC_ZHPD)
                .orderByDesc(SpcySqsxJob::getFfssj));
        if (CollectionUtils.isNotEmpty(zhpdList)){
            SpcySqsxJob zhpdJod = zhpdList.get(0);
            zhpdJod.setFfssj(new Date());
            spcySqsxJobService.updateById(zhpdJod);
        }

    }

    public Result passJobScmsXcjcAndJsspByQtHj(String sqsxid,String jobId,String opinion,String nowNode,String nextNodeDbrIds){
        String ywflCode = busService.getYwflCode(sqsxid);
        FlowConstant flowConstant1 = SpcyConstant.getFlowConstant(ywflCode);

        String nextNode = "";
        List<String> nodeList = Arrays.asList(YpscFlowConstant.NODENAME.split(","));
        boolean isExist = nodeList.contains(nowNode); // 环节名称是否存在，查验的环节里面
        if (isExist){
            // 存在
            nextNode = flowConstant1.getNextNodeName(nowNode); // 查验-下环节名称
        }else {
            // 不存在
            nextNode = flowConstant1.getSpNextNodeName(nowNode);// 审评-下环节名称
        }

        // 查验方案审批
        if (nowNode.equals(YpscFlowConstant.HJMC_FASP)) {
            nextNodeDbrIds = busService.getDbrXcjc(sqsxid);
        }
        // 审评方案审批
        if (nowNode.equals(YpscFlowConstant.HJMC_SP_FASP)) {
            nextNodeDbrIds = busService.getDbrJssp(sqsxid);
        }

        Result result = this.passJob(sqsxid, jobId, opinion, nextNode, nextNodeDbrIds);
        if (!result.isSuccess()){
            return result;
        }

        return Result.ok("succss");
    }

    // 现场检查模式+技术审评模式-提交流程-现场检查
    public Result passJobScmsXcjcAndJsspByXcjc(SpcySqsxBasic sqsx,String sqsxid,String jobId,String opinion,String nextNodeDbrIds){
        FlowConstant flowConstant1 = SpcyConstant.getFlowConstant(sqsx.getFywflcode());

        String fcywczt = sqsx.getFcywczt();
        String fspwczt = sqsx.getFspwczt();

        boolean passFlag = false;
        if (fcywczt.equals("否") && fspwczt.equals("否")){
            passFlag = true;
        }
        String jobNodeName = busService.getJobNodeName(jobId); // 当前环节

        if (!passFlag) {
            // 判断 综合评定环节-是否存在
            List<SpcySqsxJob> zhpdList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                    .eq(SpcySqsxJob::getFpid, sqsxid)
                    .eq(SpcySqsxJob::getFhjmc, YpscFlowConstant.HJMC_ZHPD)
                    .ne(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                    .like(SpcySqsxJob::getFlcmc, YpscFlowConstant.LCMC_LIKE_SPCYLC));
            if (CollectionUtils.isEmpty(zhpdList)) {
                passFlag = true;
            }
        }

        if (passFlag){
            String nextNodeName1 = flowConstant1.getNextNodeName(jobNodeName); // 查验-下环节名称
            Result result =  this.passJob(flowConstant1,sqsxid, jobId, opinion, nextNodeName1, nextNodeDbrIds);// 创建流程任务
            if (!result.isSuccess()) {
                return result;
            } else{
                sqsx.setFcywczt("是");
                spcySqsxBaiscService.updateById(sqsx);
            }
        }else {
            SpcySqsxJob job = spcySqsxJobService.getById(jobId);
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
            if (currentUser == null){
                return Result.error("登录失效，请重新登录");
            }
            Result result = this.doJob(job, currentUser, opinion);
            if (!result.isSuccess()){
                return result;
            }else {
                sqsx.setFcywczt("是");
                spcySqsxBaiscService.updateById(sqsx);

                // 综合评定环节-更新时间
                this.updateZhpdJobFssj(sqsxid);
            }
        }

        // 现场检查
        if (jobNodeName.equals(YpscFlowConstant.HJMC_XCJC)){
            busService.updateCyUserGzzt(sqsxid); // 更新人员工作状态

            sqsx.setFcywczt("是");
            spcySqsxBaiscService.updateById(sqsx);
        }

        return Result.ok("success");
    }

    // 现场检查模式+技术审评模式-提交流程-技术审评
    public Result passJobScmsXcjcAndJsspByJssp(SpcySqsxBasic sqsx,String sqsxid,String jobId,String opinion,String nextNodeDbrIds){
        FlowConstant flowConstant1 = SpcyConstant.getFlowConstant(sqsx.getFywflcode());

        String fcywczt = sqsx.getFcywczt();
        String fspwczt = sqsx.getFspwczt();

        boolean passFlag = false;
        if (fcywczt.equals("否") && fspwczt.equals("否")){
            passFlag = true;
        }

        if (!passFlag) {
            // 判断 综合评定环节-是否存在
            List<SpcySqsxJob> zhpdList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                    .eq(SpcySqsxJob::getFpid, sqsxid)
                    .eq(SpcySqsxJob::getFhjmc, YpscFlowConstant.HJMC_ZHPD)
                    .ne(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                    .like(SpcySqsxJob::getFlcmc, YpscFlowConstant.LCMC_LIKE_SPCYLC));
            if (CollectionUtils.isEmpty(zhpdList)) {
                passFlag = true;
            }
        }

        String jobNodeName = busService.getJobNodeName(jobId);

        if (passFlag){
            String nextNodeName1 = flowConstant1.getSpNextNodeName(jobNodeName); // 审评-下环节名称
            Result result =  this.passJob(flowConstant1,sqsxid, jobId, opinion, nextNodeName1, nextNodeDbrIds);// 创建流程任务
            if (!result.isSuccess()) {
                return result;
            } else{
                sqsx.setFspwczt("是");
                spcySqsxBaiscService.updateById(sqsx);
            }
        }else {
            SpcySqsxJob job = spcySqsxJobService.getById(jobId);
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
            if (currentUser == null){
                return Result.error("登录失效，请重新登录");
            }
            Result result = this.doJob(job, currentUser, opinion);
            if (!result.isSuccess()){
                return result;
            }else {
                sqsx.setFspwczt("是");
                spcySqsxBaiscService.updateById(sqsx);
            }
        }

        // 技术审评
        if (jobNodeName.equals(YpscFlowConstant.HJMC_JSSP)){
            busService.updateSpUserGzzt(sqsxid); // 更新人员工作状态

        }

        return Result.ok("success");
    }

    // 现场检查模式+技术审评模式-提交流程-综合审查
    public Result passJobScmsXcjcAndJsspByZhsc(SpcySqsxBasic sqsx,String sqsxid,String jobId,String opinion,String nextNodeDbrIds){
        String fcywczt = sqsx.getFcywczt();
        String fspwczt = sqsx.getFspwczt();
        boolean passFlag = false;
        String msg = "";
        if (fcywczt.equals("是") && fspwczt.equals("否")){
            msg = "提交当前环节任务失败，技术审评环节没有提交，需要现场检查和技术审评全部提交，才能进行。";
        }else if (fcywczt.equals("否") && fspwczt.equals("是")){
            msg = "提交当前环节任务失败，现场检查环节没有提交，需要现场检查和技术审评全部提交，才能进行。";
        }else {
            passFlag = true;
        }
        if (passFlag) {
            Result result = this.passJob(sqsxid, jobId, opinion, "", nextNodeDbrIds);
            if (!result.isSuccess()) {
                return result;
            }
        }else {
            return Result.error(msg);
        }
        return Result.ok("success");
    }

    /**
     * 退回流程 - 指定环节
     */
    public void returnZdJob(SpcyReturnVo spcyReturnVo){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            throw new BusinessException("登录已失效，请重新登录！");
        }

        if (StringUtils.isBlank(spcyReturnVo.getJobid())){
            throw new BusinessException("流程任务参数为空！");
        }

        if (StringUtils.isBlank(spcyReturnVo.getOpinion())){
            throw new BusinessException("请填写退回意见！");
        }

        if (StringUtils.isBlank(spcyReturnVo.getShjid())){
            throw new BusinessException("请选择退回环节！");
        }

        // 当前环节
        SpcySqsxJob currentJob = spcySqsxJobService.getById(spcyReturnVo.getJobid());
        if (currentJob == null){
            throw new BusinessException("当前流程任务不存在！");
        }

        String sqsxid = currentJob.getFpid();
        String fscms = SpcyConstant.SQSX_SCMS_XCJC;; // 审查模式
        boolean isExistSqsx = false; // 是否存在申请事项
        SpcySqsxBasic sqsx = null;
        if (StringUtils.isNotBlank(sqsxid)) {
            sqsx = spcySqsxBaiscService.getById(sqsxid);
            if (sqsx != null){
                isExistSqsx = true;
            }
            if (sqsx != null && StringUtils.isNotBlank(sqsx.getFscms())){
                fscms = sqsx.getFscms();
            }
        }

        // 退回环节
        SpcySqsxJob previousJob = null;
        String previousNode = ""; // 退回环节

        // 方案制作/技术审评
        boolean isFazzJssp = false;
        if (spcyReturnVo.getShjid().equals(YlqxFlowConstant.JSSP_FAZZ_ID)){
            // 是否存在 方案制作 待办
            List<SpcySqsxJob> fazzJobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                    .eq(SpcySqsxJob::getFpid, sqsxid)
                    .eq(SpcySqsxJob::getFhjmc, YlqxFlowConstant.HJMC_FAZZ)
                    .ne(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                    .orderByDesc(SpcySqsxJob::getFfssj));
            if (CollectionUtils.isNotEmpty(fazzJobList)){
                isFazzJssp = true;
                // 存在待办 方案制作
                // 是否存在待办现场检查
                List<SpcySqsxJob> xcjcJobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                        .eq(SpcySqsxJob::getFpid, sqsxid)
                        .eq(SpcySqsxJob::getFhjmc, YlqxFlowConstant.HJMC_XCJC)
                        .ne(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                        .orderByDesc(SpcySqsxJob::getFfssj));
                if (CollectionUtils.isNotEmpty(xcjcJobList)) {
                    // 不存在待办
                    // 存在待办

                }else{
                    // 存在待办
                    // 办结当前流程
                    String bllx = SpcyConstant.BLLX_TH + "[" + YlqxFlowConstant.HJMC_FAZZ + "]";
                    Result result = this.doJob(currentJob, currentUser, spcyReturnVo.getOpinion(), bllx);
                    if (!result.isSuccess()) {
                        throw new BusinessException(result.getMessage());
                    }else {
                        // 存在待办
                        if (isExistSqsx){
                            sqsx.setFspfazt("否");
                            sqsx.setFspwczt("否");
                            spcySqsxBaiscService.updateById(sqsx);
                        }
                    }
                }

            }else {
                // 不存在待办
                isFazzJssp = true;
                previousJob = fazzJobList.get(0);
                previousNode = previousJob.getFhjmc();
            }
        }

        if (!isFazzJssp) {
            // 上一环节
            previousJob = spcySqsxJobService.getById(spcyReturnVo.getShjid());
            if (currentJob == null) {
                throw new BusinessException("当前流程任务不存在！");
            }
            previousNode = previousJob.getFhjmc();

            if (StringUtils.isBlank(previousJob.getFblzt()) || !previousJob.getFblzt().equals(SpcyConstant.BLZT_YB)) {
                throw new BusinessException("当前流程环节[" + previousNode + "]已经处于待办无法退回！");
            }

            String bllx = SpcyConstant.BLLX_TH + "[" + previousNode + "]";
            Result result = this.doJob(currentJob, currentUser, spcyReturnVo.getOpinion(), bllx);
            if (!result.isSuccess()) {
                throw new BusinessException(result.getMessage());
            }
        }

        /**
         * 创建退回环节流程任务
         */
        if (previousJob != null) {
            SpcySqsxJob newPreviousJob = new SpcySqsxJob();
            BeanUtil.copyProperties(previousJob, newPreviousJob);
            newPreviousJob.setId(GetUuIdUtils.NotReplaceUuId());

            if (StringUtils.isBlank(newPreviousJob.getFpid())) {
                newPreviousJob.setFpid(sqsxid);
            }

            newPreviousJob.setFbllx(SpcyConstant.BLLX_DB);
            newPreviousJob.setFblzt(SpcyConstant.BLZT_TH);
            newPreviousJob.setFfssj(new Date());
            newPreviousJob.setFblyj("");
            newPreviousJob.setFblr("");
            newPreviousJob.setFblsj(null);
            newPreviousJob.setFblbm("");

            newPreviousJob.setFsyts("0");
            newPreviousJob.setFsfcs("");

         /*newPreviousJob.setFjzblsj(null);
         newPreviousJob.setFksyts("0");*/

            newPreviousJob.setFksyts(flowJobBlsxService.getSqsxSysx(sqsxid));
            newPreviousJob.setFjzblsj(flowJobBlsxService.jsNodeJzrq(newPreviousJob.getFfssj(), newPreviousJob.getFksyts()));

            newPreviousJob.setFztsyts("0");
            newPreviousJob.setFztkssj(null);
            newPreviousJob.setFztjssj(null);
            newPreviousJob.setFtszt("");

            newPreviousJob.setFfsrid(currentUser.getId());
            newPreviousJob.setFfsrmc(currentUser.getName());
            boolean save = spcySqsxJobService.save(newPreviousJob);
            if (save) {
                // 发送退回短信
                String nextNodeSmsContent = busService.getNextNodeSmsContent(newPreviousJob, null);
                busService.sendNextNodeSmsContent(newPreviousJob, nextNodeSmsContent);
            }
        }

        if (fscms.equals(SpcyConstant.SQSX_SCMS_XCJC)){

            if (StringUtils.isNotBlank(previousNode) && previousNode.equals(YpscFlowConstant.HJMC_FAZZ) && isExistSqsx){
                sqsx.setFcyfazt("否");
                sqsx.setFcywczt("否");
                spcySqsxBaiscService.updateById(sqsx);
            }

            if (StringUtils.isNotBlank(previousNode) && previousNode.equals(YpscFlowConstant.HJMC_XCJC) && isExistSqsx){
                sqsx.setFcywczt("否");
                spcySqsxBaiscService.updateById(sqsx);
            }

        }else if (fscms.equals(SpcyConstant.SQSX_SCMS_JSSC)){

            if (StringUtils.isNotBlank(previousNode) && previousNode.equals(YpscFlowConstant.HJMC_FAZZ) && isExistSqsx){
                sqsx.setFspfazt("否");
                sqsx.setFspwczt("否");
                spcySqsxBaiscService.updateById(sqsx);
            }

        }else if (fscms.equals(SpcyConstant.SQSX_SCMS_SCJCANBJSSC)){

            if (StringUtils.isNotBlank(previousNode) && previousNode.equals(YpscFlowConstant.HJMC_FAZZ) && isExistSqsx && !isFazzJssp){
                sqsx.setFcyfazt("否");
//                sqsx.setFspfazt("否");
                spcySqsxBaiscService.updateById(sqsx);
            }

            if (StringUtils.isNotBlank(previousNode) && previousNode.equals(YpscFlowConstant.HJMC_FAZZ) && isExistSqsx && isFazzJssp){
                sqsx.setFspfazt("否");
                sqsx.setFspwczt("否");
                spcySqsxBaiscService.updateById(sqsx);
            }

            if (StringUtils.isNotBlank(previousNode) && previousNode.equals(YpscFlowConstant.HJMC_XCJC) && isExistSqsx){
                sqsx.setFcywczt("否");
                spcySqsxBaiscService.updateById(sqsx);
            }

        }

    }

    /**
     * 政务服务系统 - 退回功能
     */
    public void zwfwxtReturnJob(XzspJkApiJsywbmthParamVo jsywbmthVo){
        String access_token = jsywbmthVo.getAccess_token();
        if (StringUtils.isBlank(access_token)){
            throw new BusinessException("access_token不能为空！");
        }
        XzspJkApiJsywbmthVo param = jsywbmthVo.getParam();

        if (StringUtils.isBlank(param.getSxsqjbxx_id())){
            throw new BusinessException("操作失败，事项申请基本信息id为空！");
        }

        if (StringUtils.isBlank(param.getThsj())){
            throw new BusinessException("操作失败，退回时间为空！");
        }

        // 申请事项
        List<SpcySqsxBasic> sqsxList = spcySqsxBaiscService.list(new LambdaQueryWrapper<SpcySqsxBasic>()
                .eq(SpcySqsxBasic::getFsxsqjbxxid, param.getSxsqjbxx_id()));

        if (CollectionUtils.isEmpty(sqsxList)){
            throw new BusinessException("操作失败，当前事项申请信息不存在！");
        }
        log.error("接受业务部门退回接口");

        String fywflcode = sqsxList.get(0).getFywflcode();
        String sqsxid = sqsxList.get(0).getId();


        SpcySqsxJob previousJob = null; // 指定退回环节-流程任务
        String returnJobNode = ""; // 指定退回环节 - 各科室
        if (StringUtils.isBlank(fywflcode)){
            // 综合评定 - 环节
            returnJobNode = SpcyFlowConstant.HJMC_ZHPD;

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            // 医疗器械 - 科室复核
            returnJobNode = SpcyFlowConstant.HJMC_ZHPD;

        }else {
            // 综合评定
            returnJobNode = SpcyFlowConstant.HJMC_ZHPD;
        }

        // 查询是否存在待办流程
        List<SpcySqsxJob> jobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFpid, sqsxid)
                .ne(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                .like(SpcySqsxJob::getFlcmc, YpscFlowConstant.LCMC_LIKE_SPCYLC)
                .orderByDesc(SpcySqsxJob::getFfssj));

        if (CollectionUtils.isEmpty(jobList)){
           // 综合评定 - 环节
            List<SpcySqsxJob> zdJobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                    .eq(SpcySqsxJob::getFpid, sqsxid)
                    .eq(SpcySqsxJob::getFhjmc, returnJobNode)
                    .eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                    .like(SpcySqsxJob::getFlcmc, YpscFlowConstant.LCMC_LIKE_SPCYLC)
                    .orderByDesc(SpcySqsxJob::getFfssj));
            if (CollectionUtils.isNotEmpty(zdJobList)){
                previousJob = zdJobList.get(0);
            }else {
                log.error("接受业务部门退回接口: 不存在指定环节【"+returnJobNode+"】");
                return;
            }
        }else {
            log.error("接受业务部门退回接口: 发送短信-指定环节【"+returnJobNode+"】");

            String nextNodeSmsContent = "";
            if (StringUtils.isNotBlank(param.getThyj())) {
                nextNodeSmsContent += "政务服务退回事项-退回意见："+param.getThyj();
            }
            if (StringUtils.isNotBlank(param.getThsj())){
                nextNodeSmsContent += "退回时间："+param.getThsj();
            }
            busService.sendNextNodeSmsContent(jobList.get(0),nextNodeSmsContent);

            return;
        }

        if (previousJob != null) {
            /**
             * 创建退回环节流程任务
             */
            SpcySqsxJob newPreviousJob = new SpcySqsxJob();
            BeanUtil.copyProperties(previousJob, newPreviousJob);
            newPreviousJob.setId(GetUuIdUtils.NotReplaceUuId());

            if (StringUtils.isBlank(newPreviousJob.getFpid())) {
                newPreviousJob.setFpid(sqsxid);
            }

            newPreviousJob.setFbllx(SpcyConstant.BLLX_DB);
            newPreviousJob.setFblzt(SpcyConstant.BLZT_TH);
            newPreviousJob.setFfssj(new Date());

            newPreviousJob.setFblyj("");
            newPreviousJob.setFblr("");
            newPreviousJob.setFblsj(null);
            newPreviousJob.setFblbm("");

            newPreviousJob.setFsyts("0");
            newPreviousJob.setFsfcs("");

            newPreviousJob.setFksyts(flowJobBlsxService.getSqsxSysx(sqsxid));
            newPreviousJob.setFjzblsj(flowJobBlsxService.jsNodeJzrq(newPreviousJob.getFfssj(), newPreviousJob.getFksyts()));

            newPreviousJob.setFztsyts("0");
            newPreviousJob.setFztkssj(null);
            newPreviousJob.setFztjssj(null);
            newPreviousJob.setFtszt("");
            if (StringUtils.isNotBlank(param.getThyj())) {
                newPreviousJob.setFthyj(param.getThyj());
                newPreviousJob.setFblyj(param.getThyj());
            }

            newPreviousJob.setFfsrid(sqsxList.get(0).getFxzspysrid()); // 发送人id
            newPreviousJob.setFfsrmc(sqsxList.get(0).getFxzspysrmc() + "/" + sqsxList.get(0).getFxzspysrbm()); // 发送人名称
            newPreviousJob.setFlcsfkb(SpcyFlowConstant.JOB_LCSFKB_KB);
            boolean save = spcySqsxJobService.save(newPreviousJob);
            if (save) {
                // 发送退回短信
                String nextNodeSmsContent = busService.getNextNodeSmsContent(newPreviousJob, null);
                if (StringUtils.isNotBlank(param.getThyj())) {
                    nextNodeSmsContent += "政务服务退回事项-退回意见：" + param.getThyj();
                }
                if (StringUtils.isNotBlank(param.getThsj())) {
                    nextNodeSmsContent += "，退回时间：" + param.getThsj();
                }
                busService.sendNextNodeSmsContent(newPreviousJob, nextNodeSmsContent);
            }
        }
    }

    /**
     * 查询审评查验流程当前环节-上一环节
     * @param jobid 流程任务id
     * @param isZdhj 是否指定环节
     * @return
     */
    public List<Map<String,String>> getCurrentJobPrevious(String jobid,boolean isZdhj){
        if (StringUtils.isBlank(jobid)){
            throw new BusinessException("流程任务参数为空！");
        }

        SpcySqsxJob currentJob = spcySqsxJobService.getById(jobid);
        if (currentJob == null){
            throw new BusinessException("当前流程任务不存在！");
        }

        String fywflcode = "";
        String fpid = currentJob.getFpid(); // 关联业务id
        String flcmc = currentJob.getFlcmc(); // 所属流程
        String currentNode = currentJob.getFhjmc(); // 当前环节

        if (StringUtils.isBlank(flcmc)){
            fywflcode = flowConstant.getFywflcode();

        }else {
            if (flcmc.equals(flowConstant.getFlowName())){
                fywflcode = flowConstant.getFywflcode();

            }else if (flcmc.equals(ypzcFlowConstant.getFlowName())){
                fywflcode = ypzcFlowConstant.getFywflcode();

            }else if (flcmc.equals(ypltFlowConstant.getFlowName())){
                fywflcode = ypltFlowConstant.getFywflcode();

            }else if (flcmc.equals(ylqxFlowConstant.getFlowName())){
                fywflcode = ylqxFlowConstant.getFywflcode();

            }else if (flcmc.equals(hzpFlowConstant.getFlowName())){
                fywflcode = hzpFlowConstant.getFywflcode();

            }
        }

        String fscms = SpcyConstant.SQSX_SCMS_XCJC;; // 审查模式
        SpcySqsxBasic sqsx = null;
        if (StringUtils.isNotBlank(fpid)) {
            sqsx = spcySqsxBaiscService.getById(fpid);
            if (sqsx != null && StringUtils.isNotBlank(sqsx.getFscms())){
                fscms = sqsx.getFscms();
            }
        }

        List<Map<String,String>> ybNodeList = new ArrayList<>(); // 退回环节数组
        String previousNode = this.getPreviousNodeByYwflcode(fywflcode, currentNode,fscms,fpid,flcmc); // 上一环节

        // 指定环节
        if (isZdhj){
            List<SpcySqsxJob> preJobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                    .eq(SpcySqsxJob::getFpid, fpid)
                    .eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                    .eq(SpcySqsxJob::getFlcmc, flcmc)
                    .orderByAsc(SpcySqsxJob::getFfssj));
            if (CollectionUtils.isNotEmpty(preJobList)){
                for (SpcySqsxJob job : preJobList){

                    // 判断是否存在一样的环节名称，存在跳出当次循环
                    boolean isExistNode = false;
                    if (CollectionUtils.isNotEmpty(ybNodeList)){
                        for (Map<String,String> disMap : ybNodeList){
                            if (disMap.get("fhjmc").equals(job.getFhjmc())){
                                isExistNode = true;
                                break;
                            }
                        }
                    }

                    if (isExistNode){
                        continue;
                    }

                    Map<String, String> map = new HashMap<>();
                    map.put("id", job.getId());
                    map.put("fhjmc", job.getFhjmc());
                    ybNodeList.add(map);

                }

            }


        }else {
            // 查询上一环节是否存在
            List<SpcySqsxJob> preJobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                    .eq(SpcySqsxJob::getFpid, fpid)
                    .eq(SpcySqsxJob::getFhjmc, previousNode)
                    .eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                    .eq(SpcySqsxJob::getFlcmc, flcmc)
                    .orderByDesc(SpcySqsxJob::getFfssj));

            if (CollectionUtils.isNotEmpty(preJobList)){
                Map<String,String> map = new HashMap<>();
                map.put("id",preJobList.get(0).getId());
                map.put("fhjmc",preJobList.get(0).getFhjmc());
                ybNodeList.add(map);
            }
        }

        // 现场检查+技术审评 处于综合评定
        if (fscms.equals(SpcyConstant.SQSX_SCMS_SCJCANBJSSC) && currentNode.equals(YlqxFlowConstant.HJMC_ZHSC)){

            Map<String,String> map = new HashMap<>();
            map.put("id",YlqxFlowConstant.JSSP_FAZZ_ID);
            map.put("fhjmc",YlqxFlowConstant.JSSP_FAZZ_HJMC);
            ybNodeList.add(map);

        }

        return ybNodeList;
    }

    /**
     * 获取上一环节-通过业务分类编码
     * @param ywflCode 业务分类编码
     * @param currentNode 当前环节
     * @return
     */
    private String getPreviousNodeByYwflcode(String ywflCode,String currentNode, String fscms, String fpid, String flcmc){
        String preNode = "";

        if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPSC)) {
            preNode = flowConstant.getPreviousName(currentNode);
            preNode = this.verifyFazzJobListInXcjcMs(preNode,currentNode,fpid,flcmc);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZC)){
            if (StringUtils.isBlank(fscms) || fscms.equals(SpcyConstant.SQSX_SCMS_XCJC)) {
                preNode = ypzcFlowConstant.getPreviousName(currentNode);
                preNode = this.verifyFazzJobListInXcjcMs(preNode,currentNode,fpid,flcmc);

            }else if (fscms.equals(SpcyConstant.SQSX_SCMS_JSSC)){
                preNode = ypzcFlowConstant.getSpPreviousName(currentNode);
            }else {
                preNode = ypzcFlowConstant.getPreviousName(currentNode);
            }

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPJY)){
            preNode = ypltFlowConstant.getPreviousName(currentNode);
            preNode = this.verifyFazzJobListInXcjcMs(preNode,currentNode,fpid,flcmc);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            if (StringUtils.isBlank(fscms) || fscms.equals(SpcyConstant.SQSX_SCMS_XCJC)) {
                preNode = ylqxFlowConstant.getPreviousName(currentNode);
                preNode = this.verifyFazzJobListInXcjcMs(preNode,currentNode,fpid,flcmc);

            }else if (fscms.equals(SpcyConstant.SQSX_SCMS_JSSC)){
                preNode = ylqxFlowConstant.getSpPreviousName(currentNode);
            }else {
                preNode = ylqxFlowConstant.getPreviousName(currentNode);

            }

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_HZP)){
            if (StringUtils.isBlank(fscms) || fscms.equals(SpcyConstant.SQSX_SCMS_XCJC)) {
                preNode = hzpFlowConstant.getPreviousName(currentNode);
                preNode = this.verifyFazzJobListInXcjcMs(preNode,currentNode,fpid,flcmc);

            }else if (fscms.equals(SpcyConstant.SQSX_SCMS_JSSC)){
                preNode = hzpFlowConstant.getSpPreviousName(currentNode);

            }else {
                preNode = hzpFlowConstant.getPreviousName(currentNode);
            }

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZCLC)){
            if (StringUtils.isBlank(fscms) || fscms.equals(SpcyConstant.SQSX_SCMS_XCJC)) {
                preNode = ypzcFlowConstant.getPreviousName(currentNode);
                preNode = this.verifyFazzJobListInXcjcMs(preNode,currentNode,fpid,flcmc);

            }else if (fscms.equals(SpcyConstant.SQSX_SCMS_JSSC)){
                preNode = ypzcFlowConstant.getSpPreviousName(currentNode);
            }else {
                preNode = ypzcFlowConstant.getPreviousName(currentNode);
            }

        }

        return preNode;
    }

    /**
     * 现场检查 模式下-验证是否存在方案制作的下一个环节，不存在，返回方案制作环节
     * @param preNode
     * @param currentNode
     * @param fpid
     * @param flcmc
     * @return
     */
    public String verifyFazzJobListInXcjcMs(String preNode,String currentNode, String fpid,String flcmc){
        if (StringUtils.isNotBlank(currentNode) && currentNode.equals(YpscFlowConstant.HJMC_ZHPD)){
            List<SpcySqsxJob> fashjobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                    .eq(SpcySqsxJob::getFpid, fpid)
                    .eq(SpcySqsxJob::getFhjmc, YpscFlowConstant.HJMC_FASH)
                    .eq(SpcySqsxJob::getFlcmc, flcmc));
            if (CollectionUtils.isEmpty(fashjobList)){
                List<SpcySqsxJob> fazzJobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                        .eq(SpcySqsxJob::getFpid, fpid)
                        .eq(SpcySqsxJob::getFhjmc, YpscFlowConstant.HJMC_FAZZ)
                        .eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                        .eq(SpcySqsxJob::getFlcmc, flcmc));
                if (CollectionUtils.isNotEmpty(fazzJobList)){
                    preNode = YpscFlowConstant.HJMC_FAZZ;
                }
            }
        }

        return preNode;
    }

    /**
     * 现场检查+技术审评 模式下-验证是否存在方案制作的下一个环节，不存在，返回方案制作环节
     * @param preNode
     * @param currentNode
     * @param fpid
     * @param flcmc
     * @return
     */
    public String verifyFzzJobListInXcjcAndJsspMs(String preNode,String currentNode, String fpid,String flcmc){
        if (StringUtils.isNotBlank(currentNode) && currentNode.equals(YpscFlowConstant.HJMC_ZHPD)){
            List<SpcySqsxJob> fashjobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                    .eq(SpcySqsxJob::getFpid, fpid)
                    .eq(SpcySqsxJob::getFhjmc, YpscFlowConstant.HJMC_FASH)
                    .eq(SpcySqsxJob::getFlcmc, flcmc));
            if (CollectionUtils.isEmpty(fashjobList)){
                List<SpcySqsxJob> fazzJobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                        .eq(SpcySqsxJob::getFpid, fpid)
                        .eq(SpcySqsxJob::getFhjmc, YpscFlowConstant.HJMC_FAZZ)
                        .eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_YB)
                        .eq(SpcySqsxJob::getFlcmc, flcmc));
                if (CollectionUtils.isNotEmpty(fazzJobList)){
                    preNode = YpscFlowConstant.HJMC_FAZZ;
                }
            }
        }

        return preNode;
    }

    //初始化任务
    public Result initJob(String sqsxId,CurrentUserVo currentUser){
        List<SpcySqsxJob> jobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFpid, sqsxId));
        if (CollectionUtils.isNotEmpty(jobList)){
            return Result.error("当前申请事项，已经初始化了");
        }
        Result result = this.initJob(flowConstant, sqsxId, currentUser);
        if (!result.isSuccess()){
            return result;
        }else {
            SpcySqsxJob job = (SpcySqsxJob) result.getResult();
            if (job != null) {
                String nextNodeSmsContent = busService.getNextNodeSmsContent(job, null);
                busService.sendSmsToNodeDbr(job.getFdbrid(), job.getFlcmc()+"-"+job.getFhjmc(),nextNodeSmsContent);
            }
        }

        return result;
//        return this.initJob(flowConstant,sqsxId,currentUser);
    }

    /**
     * 提交当前环节的待办记录
     *  @param sqsxId 申请事项id
     *  @param jobId 当前环节id
     *  @param opinion 办理意见
     */
    public Result passJob(String sqsxId, String jobId, String opinion,String nextNodeName,String nextNodeDbrIds){
        String ywflCode = busService.getYwflCode(sqsxId);
        //处理业务表的逻辑
        Result httpResult = new Result();
        if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPSC)) {
            httpResult = this.passJob(flowConstant, sqsxId, jobId, opinion, nextNodeName, nextNodeDbrIds);
        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZC)){
            httpResult = this.passJob(ypzcFlowConstant, sqsxId, jobId, opinion, nextNodeName, nextNodeDbrIds);
        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPJY)){
            httpResult = this.passJob(ypltFlowConstant, sqsxId, jobId, opinion, nextNodeName, nextNodeDbrIds);
        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            httpResult = this.passJob(ylqxFlowConstant, sqsxId, jobId, opinion, nextNodeName, nextNodeDbrIds);
        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_HZP)){
            httpResult = this.passJob(hzpFlowConstant, sqsxId, jobId, opinion, nextNodeName, nextNodeDbrIds);
        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZCLC)){
            httpResult = this.passJob(ypzcFlowConstant, sqsxId, jobId, opinion, nextNodeName, nextNodeDbrIds);
        }
        return httpResult;
    }

    /**
     * 办理环节
     * @param job 当前流程任务
     * @param currentUser 当前用户
     * @param opinion 意见
     * @return
     */
    private Result doJob(SpcySqsxJob job, CurrentUserVo currentUser, String opinion){
        return this.doJob(job,currentUser,opinion,null);
    }

    /**
     * 办理环节
     * @param job 当前流程任务
     * @param currentUser 当前用户
     * @param opinion 意见
     * @param returnNode 退回环节
     */
    private Result doJob(SpcySqsxJob job, CurrentUserVo currentUser, String opinion, String returnNode){
        String blrId = "";
        String blr = "";
        String blbm = "";
        if (currentUser != null) {
            blrId = currentUser.getId();
            blr = currentUser.getName();
            blbm = currentUser.getOrgName();
        }
        if (job == null){
            return Result.error("当前流程任务不存在，请重试");
        }
        if(SpcyConstant.BLZT_YB.equals(job.getFblzt())){
            return Result.error("当前任务已提交");
        }
        if(StringUtils.isBlank(job.getFdbrid())){
            return Result.error("当前任务未指定办理人");
        }
        if(!job.getFdbrid().contains(blrId)){
            return Result.error("当前登录用户无此任务的办理权限");
        }
        // 办理方式 竞争办理 、多人线性办理 、多人并行办理
        String blfs = job.getFblfs();
        if(StringUtils.isBlank(blfs)||blfs.equals(SpcyConstant.BLFS_DRJJ)){
            job.setFblr(blr);
            job.setFblsj(new Date());
            job.setFblyj(opinion);
            job.setFblzt(SpcyConstant.BLZT_YB);
            if (StringUtils.isBlank(returnNode)) {
                job.setFbllx(SpcyConstant.BLLX_BL);
            }else {
                job.setFbllx(returnNode);
            }
            job.setFblbm(blbm);

            // 审评查验流程 计算 办理天数、是否超时
            if (StringUtils.isNotBlank(job.getFlcmc()) && (
                    job.getFlcmc().contains(YpscFlowConstant.LCMC_LIKE_SPCYLC)
            )) {

                job.setFsyts(flowJobBlsxService.jsTwoDateBetweenDays(job.getFksyts(),job.getFpid(),job.getFfssj(), job.getFblsj(), job.getFztkssj(),job.getFztjssj())); // 使用天数
                job.setFsfcs(flowJobBlsxService.isDateBetweenSfcs(job.getFsyts(), job.getFcnts())); // 办理是否超时

            }else {

                job.setFsyts(flowJobBlsxService.jssygzr(job.getFfssj(), job.getFblsj())); // 使用天数
                job.setFsfcs(flowJobBlsxService.isDateBetweenSfcs(job.getFsyts(), job.getFcnts())); // 办理是否超时
            }

            spcySqsxJobService.updateById(job);
            return Result.ok(job);
        }else if(blfs.equals(SpcyConstant.BLFS_DRXX)  || blfs.equals(SpcyConstant.BLFS_DRBX)){
            //先根据多个人线性和多人并行的逻辑获得下一办理人
            String nextBlrId ="";
            String nextBlr ="";
            if(blfs.equals(SpcyConstant.BLFS_DRXX)){
                String ydbrIds[] = job.getFydbrid().split(",");
                String ydbrs[] = job.getFydbr().split(",");
                for (int i=0;i<ydbrIds.length;i++){
                    String dbrId = ydbrIds[i];
                    //不考虑办理人id重复出现（一个人办多次）的情况
                    if(dbrId.equals(blrId)){
                        if(i+1<ydbrIds.length){
                            nextBlrId=ydbrIds[i+1];
                            nextBlr=ydbrs[i+1];
                            break;
                        }
                    }
                }
            }else{
                String dbrIds[] = job.getFdbrid().split(",");
                String dbrs[] = job.getFdbr().split(",");
                for (int i=0;i<dbrIds.length;i++){
                    String dbrId = dbrIds[i];
                    String dbr = dbrs[i];
                    //不考虑办理人id重复出现（一个人办多次）的情况
                    if(!dbrId.equals(blrId)){
                        nextBlrId=nextBlrId+(StringUtils.isBlank(nextBlrId)?"":",")+dbrId;
                        nextBlr=nextBlr+(StringUtils.isBlank(nextBlr)?"":",")+dbr;
                    }
                }
            }

            //如果存在下一办理人 就继续待办，多人办理意见用@@分隔，办理时间、办理人、意见 用|分隔
            job.setFblr(blr);
            job.setFblsj(new Date());
            String blyj=(job.getFblyj()==null?"":job.getFblyj())+(StringUtils.isBlank(job.getFblyj())?"":"@@")+ DateConversionUtils.DateToString(new Date()) +"|"+blr+"|"+ opinion;
            job.setFblyj(blyj);
            if(StringUtils.isBlank(nextBlrId)){
                job.setFblzt(SpcyConstant.BLZT_YB);
                if (StringUtils.isBlank(returnNode)) {
                    job.setFbllx(SpcyConstant.BLLX_BL);
                }else {
                    job.setFbllx(returnNode);
                }
                job.setFblbm(blbm);
            }else{
                job.setFblzt(SpcyConstant.BLZT_DB);
                job.setFdbrid(nextBlrId);
                job.setFdbr(nextBlr);
            }

            // 审评查验流程 计算 办理天数、是否超时
            if (StringUtils.isNotBlank(job.getFlcmc()) && (
                    job.getFlcmc().contains(YpscFlowConstant.LCMC_LIKE_SPCYLC)
            )) {

                job.setFsyts(flowJobBlsxService.jsTwoDateBetweenDays(job.getFksyts(),job.getFpid(),job.getFfssj(), job.getFblsj(), job.getFztkssj(),job.getFztjssj())); // 使用天数
                job.setFsfcs(flowJobBlsxService.isDateBetweenSfcs(job.getFsyts(), job.getFcnts())); // 办理是否超时

            }else {

                job.setFsyts(flowJobBlsxService.jssygzr(job.getFfssj(), job.getFblsj())); // 使用天数
                job.setFsfcs(flowJobBlsxService.isDateBetweenSfcs(job.getFsyts(), job.getFcnts())); // 办理是否超时
            }

            spcySqsxJobService.updateById(job);
            return Result.ok(job);
        }
        return Result.error("待办任务未指定办理方式");
    }

    /**
     * 创建下环节流程任务
     * @param sqsxId
     * @param currentJob
     * @param currentUser
     * @param nextNodeName
     * @param nextNodeDbrIds
     * @return
     */
    public Result createNextJob(String sqsxId,SpcySqsxJob currentJob, CurrentUserVo currentUser,String nextNodeName,String nextNodeDbrIds){
        if (currentJob != null) {
            nextNodeName = getNextNodeName(currentJob.getFhjmc(), nextNodeName);
        }else {
            if (StringUtils.isBlank(nextNodeName)){
                return Result.error("下环节不能为空");
            }
        }

        if(nextNodeName.equals(YpscFlowConstant.HJMC_LCJS)){
            SpcySqsxJob job = new SpcySqsxJob();

            return Result.ok(job);
        }

        Map map = this.getNodeDbr(sqsxId,nextNodeName,nextNodeDbrIds);
        nextNodeDbrIds = map.get("dbrId").toString();
        String nextNodeDbrs = map.get("dbr").toString();
        if(StringUtils.isBlank(nextNodeDbrIds)){
            return Result.error("获取不到下一环节待办人");
        }
        SpcySqsxJob job = new SpcySqsxJob();
        job.setId(GetUuIdUtils.NotReplaceUuId());
        if(currentJob != null){
            job.setFsyhjid(currentJob.getId());
        }
        job.setFpid(sqsxId);
        job.setFhjmc(nextNodeName);
        if (currentUser != null) {
            job.setFfsrid(currentUser.getId());
            job.setFfsrmc(currentUser.getName());
        }
        job.setFfssj(new Date());

        job.setFdbrid(nextNodeDbrIds);
        job.setFdbr(nextNodeDbrs);
        job.setFydbrid(nextNodeDbrIds);
        job.setFydbr(nextNodeDbrs);

       // job.setFblfs(YpscFlowConstant.getBlfs(nextNodeName));
        job.setFblzt(SpcyConstant.BLZT_DB);
        job.setFbllx(SpcyConstant.BLLX_DB);
        job.setFlcmc(flowConstant.getFlowName());
        spcySqsxJobService.save(job);
        return Result.ok(job);
    }

    /**
     * 获取流程下环节
     * @param currentNodeName 可为空(如果为空，nextNodeName不为空)
     * @param nextNodeName 可为空(如果为空，currentNodename不为空)
     * @return
     */
    private String getNextNodeName(String currentNodeName,String nextNodeName){
        if (StringUtils.isNotBlank(nextNodeName)){
            return nextNodeName;
        }else{
           return flowConstant.getNextNodeName(currentNodeName);
        }
    }

    /*private Map getNodeDbr(String nodeName,String dbrIds){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        if(StringUtils.isNotBlank(dbrIds)){
            dbr = getDbrsByIds(dbrIds);

        }else{
            // 任务受理
            if (nodeName.equals(YpscFlowConstant.HJMC_RWSL)){
                List<GetKsKz> ksKzObject = ycSysUserService.getKsKzObject(SpcyConstant.YWFL_NAME_YPSC, SpcyConstant.GW_NAME_KZ);
                if (CollectionUtils.isNotEmpty(ksKzObject)) {
                    for (GetKsKz ksKz : ksKzObject) {
                        dbrId = ksKz.getId()+",";
                        dbr = ksKz.getRealname()+",";
                    }
                }
            }
            // 其他环节
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;

    }*/

    // 获取当前环节待办人
    public Map getNodeDbr(String sqsxid,String nodeName, String dbrIds){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        if(StringUtils.isNotBlank(dbrIds)){
            dbr = getDbrsByIds(dbrIds);
            dbrId = dbrIds;
        }else{

            // 任务录入
            if (nodeName.equals(YpscFlowConstant.HJMC_RWLR)){
                // 经办人
                Map kzMap = getKsjbrByDeptName(SpcyConstant.getYwclNameByCode(flowConstant.getFywflcode()));
                dbrId = kzMap.get("dbrId").toString();
                dbr = kzMap.get("dbr").toString();
            }

            // 任务受理
            if (nodeName.equals(YpscFlowConstant.HJMC_RWSL)){
                // 科室负责人
                Map kzMap = getKsfzrByDeptName(SpcyConstant.getYwclNameByCode(flowConstant.getFywflcode()));
                dbrId = kzMap.get("dbrId").toString();
                dbr = kzMap.get("dbr").toString();
            }

            // 方案制作
            if (nodeName.equals(YpscFlowConstant.HJMC_FAZZ)){
                // 经办人
                Map kzMap = getKsjbrByDeptName(SpcyConstant.getYwclNameByCode(flowConstant.getFywflcode()));
                dbrId = kzMap.get("dbrId").toString();
                dbr = kzMap.get("dbr").toString();
            }

            // 方案审核
            if (nodeName.equals(YpscFlowConstant.HJMC_FASH)){
                // 科室负责人
                Map kzMap = getKsfzrByDeptName(SpcyConstant.getYwclNameByCode(flowConstant.getFywflcode()));
                dbrId = kzMap.get("dbrId").toString();
                dbr = kzMap.get("dbr").toString();
            }

            // 方案审核
            if (nodeName.equals(YpscFlowConstant.HJMC_SP_FASH)){
                // 科室负责人
                Map kzMap = getKsfzrByDeptName(SpcyConstant.getYwclNameByCode(flowConstant.getFywflcode()));
                dbrId = kzMap.get("dbrId").toString();
                dbr = kzMap.get("dbr").toString();
            }

            // 方案审批
            if (nodeName.equals(YpscFlowConstant.HJMC_FASP)) {
                // 分管领导
                Map kzMap = getKsFgldByDeptName(SpcyConstant.getYwclNameByCode(flowConstant.getFywflcode()));
                dbrId = kzMap.get("dbrId").toString();
                dbr = kzMap.get("dbr").toString();
            }

            // 方案审批
            if (nodeName.equals(YpscFlowConstant.HJMC_SP_FASP)) {
                // 分管领导
                Map kzMap = getKsFgldByDeptName(SpcyConstant.getYwclNameByCode(flowConstant.getFywflcode()));
                dbrId = kzMap.get("dbrId").toString();
                dbr = kzMap.get("dbr").toString();
            }

            // 现场检查
            if (nodeName.equals(YpscFlowConstant.HJMC_XCJC)){
                Map xcjcMap = getXcjcRyBySqsxId(sqsxid,SpcyConstant.FLOW_TYPE_CY);
                dbrId = xcjcMap.get("dbrId").toString();
                dbr = xcjcMap.get("dbr").toString();
            }

            // 技术审评
            if (nodeName.equals(YpscFlowConstant.HJMC_JSSP)){
                Map xcjcMap = getXcjcRyBySqsxId(sqsxid,SpcyConstant.FLOW_TYPE_SP);
                dbrId = xcjcMap.get("dbrId").toString();
                dbr = xcjcMap.get("dbr").toString();
            }

            // 综合审查
            if (nodeName.equals(YlqxFlowConstant.HJMC_ZHSC)){
                Map zbrMap = getZbrBySqsxId(sqsxid);
                dbrId = zbrMap.get("dbrId").toString();
                dbr = zbrMap.get("dbr").toString();
            }

            // 综合评定
            if (nodeName.equals(YpscFlowConstant.HJMC_ZHPD)){
                Map zbrMap = getZbrBySqsxId(sqsxid);
                dbrId = zbrMap.get("dbrId").toString();
                dbr = zbrMap.get("dbr").toString();
            }

            // 科室复核
            if (nodeName.equals(YpscFlowConstant.HJMC_KSFH)){
                // 经办人
                Map kzMap = getKsfzrByDeptName(SpcyConstant.getYwclNameByCode(flowConstant.getFywflcode()));
                dbrId = kzMap.get("dbrId").toString();
                dbr = kzMap.get("dbr").toString();
            }

            // 移送确认
            if (nodeName.equals(YpscFlowConstant.HJMC_YSQR)) {
                // 分管领导
                Map kzMap = getKsFgldByDeptName(SpcyConstant.getYwclNameByCode(flowConstant.getFywflcode()));
                dbrId = kzMap.get("dbrId").toString();
                dbr = kzMap.get("dbr").toString();
            }

        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 获取当前环节待办人
    public Map getNodeDbr(String nodeName, String dbrIds){
        return this.getNodeDbr(null,nodeName,dbrIds);
    }

    // 根据待办人id，获取名称拼接起来
    private String getDbrsByIds(String dbrIds){
        //把待办人按id查出来  把名字拼起来
        String dbr = "";
        List<String> dbrIdsList = Arrays.asList(dbrIds.split(","));
        List<YcSysUser> sysUserList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dbrIdsList)){
            sysUserList = ycSysUserService.listByIds(dbrIdsList);
        }

        if (CollectionUtils.isNotEmpty(sysUserList)){
            for (YcSysUser user : sysUserList){
                dbr += user.getRealname() + ",";
            }
        }

        return dbr;
    }

    // 对应科室的 科室负责人
    public Map getKsfzrByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName, SpcyConstant.GW_NAME_KZ);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId()+",";
                dbr += ksKz.getRealname()+",";
            }
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 对应科室的 经办人
    public Map getKsjbrByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName, SpcyConstant.GW_NAME_JBR);

        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId()+",";
                dbr += ksKz.getRealname()+",";
            }
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 获取对应科室的 分管领导
    public Map getKsFgldByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        YcSysUser fgldUserBydept = sysUserFgldService.getFgldUserBydept(deptName);
        if (fgldUserBydept != null) {
            dbrId = fgldUserBydept.getId();
            dbr = fgldUserBydept.getRealname();
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 获取主办人
    public Map getZbrBySqsxId(String sqsxId){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        SpcySqsxBasic basic = spcySqsxBaiscService.getById(sqsxId);
        if (basic != null && StringUtils.isNotBlank(basic.getFzbr())){
            List<String> userids = Arrays.asList(basic.getFzbr().split(",|，"));
            if (CollectionUtils.isNotEmpty(userids)) {
                for (String uid : userids) {
                    YcSysUser sysUser = ycSysUserService.getById(uid);
                    if (sysUser != null) {
                        if (StringUtils.isBlank(dbrId)) {
                            dbrId += sysUser.getId();
                            dbr += sysUser.getRealname();
                        } else {
                            dbrId += "," + sysUser.getId();
                            dbr += "," + sysUser.getRealname();
                        }
                    }
                }
            }
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 获取现场检查人员
    public Map getXcjcRyBySqsxId(String sqsxId,String flowType){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        // 同意选派记录
        List<SpcySqsxXpjl> xpjlList = spcySqsxXpjlService.list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                .eq(SpcySqsxXpjl::getFsqsxid, sqsxId)
                .eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES).eq(SpcySqsxXpjl::getFspcylx,flowType));
        if (CollectionUtils.isNotEmpty(xpjlList)){
            for (SpcySqsxXpjl xpjl : xpjlList){
                String fjcyid = xpjl.getFjcyid();
                RsHcyVo rsHcyVo = new RsHcyVo();
                rsHcyVo.setId(fjcyid);
                List<RsHcyVo> rsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);
                if (CollectionUtils.isNotEmpty(rsHcyList)){
                    if (StringUtils.isNotBlank(dbrId)) {
                        dbrId += rsHcyList.get(0).getFuserid() + ",";
                        dbr += rsHcyList.get(0).getRealname() + ",";
                    }else {
                        dbrId += rsHcyList.get(0).getFuserid();
                        dbr += rsHcyList.get(0).getRealname();
                    }
                }
            }
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 获取主办人或者科室负责人
    public String getZbrHzKsfzrBySqsxid(SpcySqsxBasic sqsx){
        String dbrid = "";

        // 获取主办人
        Map zbrBySqsxId = getZbrBySqsxId(sqsx.getId());
        dbrid = zbrBySqsxId.get("dbrId").toString();

        if (StringUtils.isBlank(dbrid)) {
            // 科室负责人
            Map kzMap = getKsfzrByDeptName(SpcyConstant.getYwclNameByCode(sqsx.getFywflcode()));
            dbrid = kzMap.get("dbrId").toString();
        }

        return dbrid;
    }
    /**
     * ****************************************************【  旧的方法  】************************************************/



    /************************************【       以下流程业务功能     】*****************************************************/

    // 暂停流程任务
    public Result stopFlowJob(String sqsxid,String jobid){
        if (StringUtils.isBlank(jobid)){
            return Result.error("流程任务参数不存在，请联系管理员");
        }
        if (StringUtils.isBlank(sqsxid)){
            return Result.error("申请事项参数不存在，请联系管理员");
        }
        SpcySqsxJob job = spcySqsxJobService.getById(jobid);
        if (job == null){
            return Result.error("当前流程任务不存在，请联系管理员");
        }
        job.setFblzt("暂停");
//        spcySqsxJobService.updateById(job);

        String lognr = "暂停流程：";
        Map<String, Object> jobMap = ObjectToMapUtils.objectToMap(job);
        lognr += LogUtils.MapToStringMethod(jobMap);
        spcyLogService.addLogs(job.getFhjmc(),lognr,sqsxid, LogConstant.LOGS_SQSX_JOB,SpcyConstant.LOGS_SYS_TYPE_TJ);

        SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);
        if (sqsx == null){
            return Result.error("当前申请事项不存在，请联系管理员");
        }
        sqsx.setFlczt("流程暂停");
//        spcySqsxBaiscService.updateById(sqsx);

        String lognr2 = "暂停流程：";
        lognr2 += "【"+sqsx.getFsxmc()+"】;【"+sqsx.getFslbh()+"】;【"+sqsx.getFsqdbh()+"】;";
        spcyLogService.addLogs(job.getFhjmc(),lognr2,sqsxid,LogConstant.LOGS_SQSX_BASIC,SpcyConstant.LOGS_SYS_TYPE_TJ);

        return Result.ok("操作成功");
    }

    // 恢复暂停的流程任务
    public Result startFlowJob(String sqsxid,String jobid){
        if (StringUtils.isBlank(jobid)){
            return Result.error("流程任务参数不存在，请联系管理员");
        }
        if (StringUtils.isBlank(sqsxid)){
            return Result.error("申请事项参数不存在，请联系管理员");
        }
        SpcySqsxJob job = spcySqsxJobService.getById(jobid);
        if (job == null){
            return Result.error("当前流程任务不存在，请联系管理员");
        }
        job.setFblzt(SpcyConstant.BLZT_DB);
//        job.setFztlcsj(new Date()); // 暂停流程时间
//        spcySqsxJobService.updateById(job);

        String lognr = "恢复暂停流程：";
        Map<String, Object> jobMap = ObjectToMapUtils.objectToMap(job);
        lognr += LogUtils.MapToStringMethod(jobMap);
        spcyLogService.addLogs(job.getFhjmc(),lognr,sqsxid,LogConstant.LOGS_SQSX_JOB,SpcyConstant.LOGS_SYS_TYPE_TJ);

        SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(sqsxid);
        if (sqsx == null){
            return Result.error("当前申请事项不存在，请联系管理员");
        }
        sqsx.setFlczt("");
        Date fjzrq = sqsx.getFjzrq(); // 截止日期
        Date ztlcsj = new Date(); //暂停时间
        Date nowSj = new Date(); // 当前时间
        long betweenDay = DateUtil.between(ztlcsj, nowSj, DateUnit.DAY); // 相差天数
        int betDay = new Long(betweenDay).intValue();
        Date newJzrq = DateUtil.offset(fjzrq, DateField.DAY_OF_MONTH, betDay); // 相加天数
        sqsx.setFjzrq(newJzrq); // 新的截止日期
//        sqsx.getFztlcsj();
//        spcySqsxBaiscService.updateById(sqsx);

        String lognr2 = "恢复暂停流程：";
        lognr2 += "【"+sqsx.getFsxmc()+"】;【"+sqsx.getFslbh()+"】;【"+sqsx.getFsqdbh()+"】;";
        spcyLogService.addLogs(job.getFhjmc(),lognr2,sqsxid,LogConstant.LOGS_SQSX_BASIC,SpcyConstant.LOGS_SYS_TYPE_TJ);

        return Result.ok("操作成功");
    }


    /************************************【       以下提交流程相关操作功能      】*****************************************************/

    /**
     * 申请事项id和流程任务id为空，新增事项和第一个环节
     */
    public Result addFirstJob(YpscFlowVo ypscFlowVo,String sqsxid){
        // 新增任务受理环节任务和申请事项
        Result httpResult = addRwslJob(ypscFlowVo,sqsxid);
        if (!httpResult.isSuccess()){
            return httpResult;
        }else {
            // 新增的流程job
            SpcySqsxJob job = (SpcySqsxJob) httpResult.getResult();

            Map map = new HashMap();
            Result<Object> res = new Result<>(true);
            res.setMessage("保存成功");

            // 申请事项
//            SpcySqsxBasic basic = spcySqsxBaiscService.getById(sqsxid);

            // 发送短信，新增的待办流程任务
//            busService.sentAgentSms(job);

            Map jobConcernByJobId = busService.getJobConcernByJobId(job.getId());
            map.putAll(jobConcernByJobId);

            res.setResult(map);
            return res;
        }
    }

    // 退回流程
    public Result getReturnUpperFlowJob(ReturnFlowVo flowVo){
        String jobid = flowVo.getJobid();
        String opinion = flowVo.getOpinion();
        String shjids = flowVo.getShjids();

        if (StringUtils.isBlank(jobid)){
            return Result.error("当前流程任务id不能为空");
        }
        if (StringUtils.isBlank(opinion)){
            return Result.error("当前流程任务id不能为空");
        }
        SpcySqsxJob job = spcySqsxJobService.getById(jobid);
        if (job == null){
            return Result.error("当前流程任务不存在");
        }
        SpcySqsxBasic sqsx = spcySqsxBaiscService.getById(job.getFpid());

        // 是否-指定环节
        if (StringUtils.isNotBlank(shjids)){
            List<SpcySqsxJob> jobList = spcySqsxJobService.listByIds(Arrays.asList(shjids.split(",")));
            if (CollectionUtils.isNotEmpty(jobList)){
                String fscms = sqsx.getFscms(); // 审查模式

                boolean isDel = false; // 是否允许删除流程
                boolean scmsDel = false; // 审查模式- 查验+审评 是否可以删除流程

                for (SpcySqsxJob job1 : jobList){
                    if (StringUtils.isNotBlank(job1.getFblzt()) && (job1.getFblzt().equals(SpcyConstant.BLZT_DB)
                            || job1.getFblzt().equals(SpcyConstant.BLZT_TH))){
                        return Result.error("上环节【"+job1.getFhjmc()+"】处于【"+job1.getFblzt()+"】,无法退回");
                    }else {
                        job1.setFthyj(opinion);
                        job1.setFfssj(new Date());
                        job1.setFblzt(SpcyConstant.BLZT_TH);
                        spcySqsxJobService.updateById(job1);

                        // 综合审查
                        if (job.getFhjmc().equals(YlqxFlowConstant.HJMC_ZHSC)){
                            // 审查模式- 查验+审评
                            if (fscms.equals(SpcyConstant.SQSX_SCMS_SCJCANBJSSC)){
                                // 现场检查
                                if (job1.getFhjmc().equals(YpscFlowConstant.HJMC_XCJC)){
                                    sqsx.setFcywczt("否");
                                    spcySqsxBaiscService.updateById(sqsx);
                                }
                                // 技术审评
                                if (job1.getFhjmc().equals(YpscFlowConstant.HJMC_JSSP)){
                                    sqsx.setFspwczt("否");
                                    spcySqsxBaiscService.updateById(sqsx);
                                }
                                if (sqsx.getFcywczt().equals("否") && sqsx.getFspwczt().equals("否")){
                                    scmsDel = true;
                                }
                            }else {
                                scmsDel = true;
                            }

                        }else {
                            scmsDel = true;
                        }
                        isDel = true;
                    }
                }

                if (isDel) {
                    // 审查模式- 查验+审评
                    if (fscms.equals(SpcyConstant.SQSX_SCMS_SCJCANBJSSC)) {
                        if (scmsDel) {
                            spcySqsxJobService.removeById(jobid); // 删除当前流程任务待办
                        }
                    }else {
                        spcySqsxJobService.removeById(jobid); // 删除当前流程任务待办
                    }
                }
            }
        }else {
            // 办理状态 - 待办
            if (StringUtils.isNotBlank(job.getFblzt()) && (job.getFblzt().equals(SpcyConstant.BLZT_DB)
                    || job.getFblzt().equals(SpcyConstant.BLZT_TH))) {
                // 待办-获取上一个环节id
                String fsyhjid = job.getFsyhjid();
                SpcySqsxJob syhjJob = spcySqsxJobService.getById(fsyhjid);
                if (syhjJob != null) {
                    syhjJob.setFthyj(opinion);
                    syhjJob.setFfssj(new Date());
                    syhjJob.setFblzt(SpcyConstant.BLZT_TH);
                    spcySqsxJobService.updateById(syhjJob);

                    spcySqsxJobService.removeById(jobid); // 删除当前流程任务待办
                }
            }
        }

        if (StringUtils.isNotBlank(job.getFhjmc())){
            String currentNode = job.getFhjmc(); // 当前环节

            if (currentNode.equals(YpscFlowConstant.HJMC_FASH)){
                sqsx.setFcyfazt("否");
                spcySqsxBaiscService.updateById(sqsx);
            }

            if (currentNode.equals(YpscFlowConstant.HJMC_SP_FASH)){
                sqsx.setFspfazt("否");
                spcySqsxBaiscService.updateById(sqsx);
            }
        }

        String logNr = "退回上一环节: ";
        Map<String, Object> map = ObjectToMapUtils.objectToMap(job);
        logNr += LogUtils.MapToStringMethod(map);
        spcyLogService.addLogs(job.getFhjmc(),logNr,job.getFpid(), LogConstant.LOGS_SQSX_JOB,SpcyConstant.LOGS_SYS_TYPE_TH);
        return Result.ok("success");
    }
}
